<?php

class Authorization extends Controller {
	
	function Authorization()
	{
		parent::Controller();
	}

	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('SchoolModel');
		$data['schoolList'] = $this->SchoolModel->getAll();
		
		$this->load->view('header');
		$this->load->view('authorize-detaill-form', $data);
		$this->load->view('footer');
	}
	
	function find()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
		$this->load->view('header');
		$this->load->view('authorize-list-form');
		$this->load->view('footer');
	}
	
	function search()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $data = array();
	    
	    $data['txtName'] = $this->input->post('txtName') ? $this->input->post('txtName') : null;
	    $data['txtUsername'] = $this->input->post('txtUsername') ? $this->input->post('txtUsername') : null;
	    $this->load->model('empModel');
	    $data['result'] = $this->empModel->getByCriteria($data);
		$this->load->view('header');
		$this->load->view('authorize-list-form',$data);
		$this->load->view('footer');
	}
	
	function save()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $data['employee_id'] = $this->input->post('txtEmpID') ? $this->input->post('txtEmpID') : null;
	    $data['username'] = $this->input->post('txtUsername') ? $this->input->post('txtUsername') : null;
		$data['password'] = $this->input->post('txtPassword') ? $this->input->post('txtPassword') : null;
	    $data['first_name'] = $this->input->post('txtFirstName') ? $this->input->post('txtFirstName') : null;
		$data['last_name'] = $this->input->post('txtLastName') ? $this->input->post('txtLastName') : null;
		$data['job_position'] = $this->input->post('txtJobPostion') ? $this->input->post('txtJobPostion') : null;
		$data['email'] = $this->input->post('txtEmail') ? $this->input->post('txtEmail') : null;
		$data['telephone_no'] = $this->input->post('txtTel') ? $this->input->post('txtTel') : null;
		$data['schools'] = $this->input->post('txtSchools') ? $this->input->post('txtSchools') : null;
		
		$tutorInfoArr = $this->input->post('tutorInfo');
		$tutorContractArr = $this->input->post('tutorContract');
		$stuInfoArr = $this->input->post('studentInfo');
		$stuContractArr = $this->input->post('studentContract');
		$menuArr = $this->input->post('chkMenu');
		
		// save employee info
		$this->load->model('empModel');
		$this->load->model('empschoolmodel');
	    $this->load->model('UserModel');
	    
	    $user = array();
		$user['username'] = $data['username'];
        $user['password'] = $data['password'];
        $user['profile'] = 'employee';
       
        $isRejectUser = false;
		if($data['employee_id'] == null) {
			$result = $this->UserModel->getUserByUsername($user['username']);
			if($result == null) {
				$id = $this->empModel->add($data);
				$user['create_by'] = $this->session->userdata('username');
				$this->UserModel->addUser($user);	
			} else {
				$data['username'] = null;
				$data['password'] = null;
				$id = $this->empModel->add($data);
				// reject
				$isRejectUser = true;
			}
		} else {
			$id = $data['employee_id'];
			$tmp = $this->empModel->getByID($id);
			
			if($data['username'] == $tmp['username']) {
				if($data['password'] != null) {
					$this->empModel->updateInfo($data);
					$user['update_by'] = $this->session->userdata('username');
					$this->UserModel->updateUser($user);
				} 
			} else {
				$result = $this->UserModel->getUserByUsername($user['username']);
				if($result == null) {
					$this->empModel->updateInfo($data);
					$user['update_by'] = $this->session->userdata('username');
					$this->UserModel->deleteUser($tmp['username']);
					$this->UserModel->addUser($user);
				} else {
//					$data['username'] = null;
//					$data['password'] = null;
					$this->empModel->updateInfo($data);
					
					// reject
					$isRejectUser = true;
				}
			}
			
			if($data['first_name'] != $tmp['first_name'] || $data['last_name'] != $tmp['last_name'] || $data['job_position'] != $tmp['job_position'] ||
			   $data['email'] != $tmp['email'] || $data['telephone_no'] != $tmp['telephone_no'] || $data['schools'] != $tmp['schools'] ) {
				
				$this->empModel->updateInfo($data);
			}
		}
		
		if(isset($id)) {
			// save permission
			$this->empschoolmodel->delete($id);
			$this->load->model('SchoolModel');
			$schoolList = $this->SchoolModel->getAll();
			foreach ($schoolList as $s) {
				$saveData = array();
				$saveData['employee_id'] = $id;
				$saveData['school_id'] = $s->school_id;
				if(empty($tutorInfoArr) == false && in_array($s->school_id, $tutorInfoArr)) {
					$saveData['tutor_information'] = 'Y';
				} else {
					$saveData['tutor_information'] = 'N';
				}
				if(empty($tutorContractArr) == false && in_array($s->school_id, $tutorContractArr)) {
					$saveData['tutor_contract'] = 'Y';
				} else {
					$saveData['tutor_contract'] = 'N';
				}
				if(empty($stuInfoArr) == false && in_array($s->school_id, $stuInfoArr)) {
					$saveData['student_information'] = 'Y';
				} else {
					$saveData['student_information'] = 'N';
				}
				if(empty($stuContractArr) == false && in_array($s->school_id, $stuContractArr)) {
					$saveData['student_contract'] = 'Y';
				} else {
					$saveData['student_contract'] = 'N';
				}
				
				if(in_array('Y', $saveData)) {
					$this->empschoolmodel->add($saveData);
				}
			}
			$this->empModel->clearSchool($id);
			if(isset($menuArr)) {
				for($i = 0; $i < count($menuArr); $i++) {
					if($menuArr[$i] != null)
						$this->empModel->updateSchool($id, $menuArr[$i]);
				}
			}
		}
		
		$d = array();
		$d['msg'] = 'บันทึกข้อมูลเรียบร้อย';
		if(isset($isRejectUser) && $isRejectUser) {
			$d['msg'] = "Username ที่กำหนดถูกใช้แล้ว กรุณาเปลี่ยน username ใหม่";
		}		
		$data['form_status'] = 'add';
		$this->load->model('SchoolModel');
		$data['schoolList'] = $this->SchoolModel->getAll();
		
		$this->load->view('header');
		$this->load->view('employee-list-form', $data);
		$this->load->view('footer', $d);
	}
	
	
	function reject()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $data['employee_id'] = $this->input->post('txtEmpID') ? $this->input->post('txtEmpID') : null;
		
		// save employee info
		$this->load->model('empModel');
		$this->load->model('UserModel');
		$this->load->model('empschoolmodel');
		
		$tmp = $this->empModel->getByID($data['employee_id']);
		$this->empModel->delete($data);
	    $this->UserModel->deleteUser($tmp['username']);
        $this->empschoolmodel->delete( $data['employee_id']);
        
		$d = array();
		if(isset($isRejectUser) && $isRejectUser) {
			$d['msg'] = "ลบข้อมูลเรียบร้อย";
		}		
		
		$this->load->view('header');
		$this->load->view('authorize-list-form', $d);
		$this->load->view('footer');
	}
	
	function detail($id)
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
		// save employee info
		$this->load->model('empModel');
		$this->load->model('empschoolmodel');
		$data = array();
		
		$data = $this->empModel->getByID($id);
		$data['permission'] = $this->empschoolmodel->getByID($id);
		$this->load->model('SchoolModel');
		$data['schoolList'] = $this->SchoolModel->getAll();
		$this->load->view('header');
		$this->load->view('authorize-detaill-form', $data);
		$this->load->view('footer');
	}
	
}

?>